package com.gin.no_gay_alliance.fragment.item;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.gin.nga.enums.ItemType;
import com.gin.nga.params.nuke.item.ItemStoreParam;
import com.gin.nga.response.body.nuke.ItemBody;
import com.gin.no_gay_alliance.R;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.callback.Callback;
import com.gin.no_gay_alliance.customview.ItemView;
import com.gin.no_gay_alliance.databinding.FragmentStoreBinding;
import com.gin.no_gay_alliance.param.ItemParam;
import com.gin.no_gay_alliance.utils.ViewUtils;

/**
 * 道具商店
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/7 16:37
 **/
public class StoreFragment extends BaseItemFragment<FragmentStoreBinding> implements RadioGroup.OnCheckedChangeListener, Callback<ItemBody> {
    public static final String TAG = StoreFragment.class.getSimpleName();
    private ItemType type = null;


    /**
     * 单选框切换时, 执行查询
     */
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // 查询所有
        if (checkedId == R.id.radio_all) {
            type = null;
        }
        if (checkedId == R.id.radio_to_user) {
            type = ItemType.TO_USER;
        }
        if (checkedId == R.id.radio_to_reply) {
            type = ItemType.TO_REPLY;
        }
        if (checkedId == R.id.radio_medal) {
            type = ItemType.MEDAL;
        }
        if (checkedId == R.id.radio_activation_code) {
            type = ItemType.ACTIVATION_CODE;
        }
        // 从头加载
        load(false);
    }

    @Override
    protected ItemView.Params buildParams(ItemParam param) {
        if (param.isToUser()) {
            return new ItemView.Params(ItemView.Action.BUY_USE, param.userId);
        } else if (param.isToReply()) {
            return new ItemView.Params(ItemView.Action.BUY_USE, param.topicId, param.replyId);
        } else {
            return new ItemView.Params(ItemView.Action.BUY);
        }
    }

    /**
     * 提供布局中的 RecyclerView
     *
     * @return RecyclerView
     */
    @NonNull
    @Override
    protected RecyclerView initRecyclerView() {
        return b.recyclerView;
    }

    /**
     * 发送请求
     *
     * @param more 是否为加载更多, 否时从第一页开始
     */
    @Override
    protected void sendRequest(boolean more) {
        ItemStoreParam param = new ItemStoreParam(page, type, null);
        MyApplication.app.managerFactory.getItemInventoryManager().itemStore(false, param, this);
    }

    /**
     * onCreateView 的后续操作
     */
    @Override
    protected void onCreateView() {
        super.onCreateView();
        b.radioGroup.setOnCheckedChangeListener(this);

        // 配置单选框
        if (itemParam.isToUser()) {
            b.radioGroup.check(R.id.radio_to_user);
        } else if (itemParam.isToReply()) {
            b.radioGroup.check(R.id.radio_to_reply);
        } else {
            b.radioGroup.check(R.id.radio_all);
            ViewUtils.setVisibility(View.VISIBLE
                    , b.radioAll
                    , b.radioToUser
                    , b.radioMedal
                    , b.radioToReply
                    , b.radioActivationCode
            );
        }

    }

    public static StoreFragment newInstance(ItemParam param) {
        StoreFragment fragment = new StoreFragment();
        fragment.setItemParam(param);
        return fragment;
    }
}
